package com.sdh.onlinemarket.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sdh.onlinemarket.common.annotation.AdminToken;
import com.sdh.onlinemarket.common.dto.ClassicDTO;
import com.sdh.onlinemarket.common.dto.TopClassicDTO;
import com.sdh.onlinemarket.common.lang.Result;
import com.sdh.onlinemarket.entity.Item;
import com.sdh.onlinemarket.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 施德昊
 * @since 2021-11-21
 */
@Api(tags = "商品相关模块")
@RestController
@RequestMapping("/item")
@Log4j2
public class ItemController {

    @Autowired
    ItemService itemService;

    @AdminToken
    @ApiOperation(value = "添加商品")
    @PostMapping("/setitem")
    @ResponseBody
    public Result setitem(@RequestBody Item item){
        if(item.getItemname()==null||item.getIprice()==null||item.getIdescrip()==null||item.getSubclassic()==null){
            return Result.fail("参数错误");
        }
        System.out.println(item);
        System.out.println("---------------------------------"+LocalDateTime.now());
        item.setIstatus(0);
        if(itemService.save(item)){
            System.out.println("请求添加商品了！"+LocalDateTime.now());
            return Result.succ("成功！");
        }else {
            System.out.println("请求添加商品失败！"+LocalDateTime.now());
            return Result.fail("输入有误！");
        }
    }

    @AdminToken
    @ApiOperation(value = "修改商品")
    @PostMapping("/modifyitem")
    @ResponseBody
    public Result modifyitem(@RequestBody Item item){
        if(item.getItemname()==null||item.getIprice()==null||item.getIdescrip()==null||item.getSubclassic()==null){
            return Result.fail("参数错误");
        }
        item.setIstatus(0);
        if(itemService.update(item,new QueryWrapper<Item>().eq("itemid",item.getItemid()))){
            System.out.println("成功修改商品了！"+LocalDateTime.now());
            return Result.succ("成功！");
        }else {
            System.out.println("修改商品失败！"+LocalDateTime.now());
            return Result.fail("输入有误！");
        }
    }


    @ApiOperation(value = "获取商品列表")
    @PostMapping("/getitemlist")
    @ResponseBody
    public Result getItemList(){
        return Result.succ("success",itemService.list());
    }


    @ApiOperation(value = "获取大类列表")
    @PostMapping("/getbigtype")
    @ResponseBody
    public Result getBigType(){
        List<Item> itemList = itemService.selectBigType();

        System.out.println(itemList);

        if(itemList == null||itemList.size() == 0){
            return Result.fail("item not found");
        }

        return Result.succ("success",itemList);
    }

    @ApiOperation(value = "根据大类获取子类列表")
    @PostMapping("/getitembytype")
    @ResponseBody
    public Result getItembytype1(String classic){

//        List<Item> itemList = itemService.list(new QueryWrapper<Item>()
//                .select("distinct subclassic ,classic ")
//                .groupBy("subclassic")
//                .eq("classic",classic));

//                List<Item> itemList = itemService.list(new QueryWrapper<Item>()
//                .eq("classic",classic));

        List<Item> itemList = itemService.selectDisSub(classic);

        System.out.println(itemList);

        if(itemList == null||itemList.size() == 0){
            return Result.fail("item not found");
        }

        List<TopClassicDTO> topClassicDTOList = new ArrayList<>();
        for (Item itemt:itemList) {
            TopClassicDTO topClassicDTO = new TopClassicDTO();
            BeanUtils.copyProperties(itemt,topClassicDTO);
            System.out.println(topClassicDTO);
            topClassicDTOList.add(topClassicDTO);
        }

        return Result.succ("success",topClassicDTOList);
    }

    @ApiOperation(value = "根据大类和子类获取商品列表")
    @PostMapping("/getitembytype2")
    @ResponseBody
    public Result getItembytype2(@RequestBody ClassicDTO classicDTO){
        List<Item> itemList = itemService.list(new QueryWrapper<Item>().eq("classic",classicDTO.getClassic())
                .eq("subclassic",classicDTO.getSubclassic()));

        if(itemList == null||itemList.size() == 0){
            return Result.fail("item not found");
        }

        return Result.succ("success",itemList);
    }

    @ApiOperation(value = "获取一个商品的详细信息")
    @PostMapping("/getsingalitem")
    @ResponseBody
    public Result getSingalItem(String itemid){
        Item item = itemService.getOne(new QueryWrapper<Item>().eq("itemid",itemid));
        if(item == null){
            return Result.fail("item not found");
        }
        return Result.succ("success",item);
    }

    @AdminToken
    @ApiOperation(value = "删除一个商品")
    @PostMapping("/itemdel")
    @ResponseBody
    public Result DelItem(String itemid){
        Item item = itemService.getOne(new QueryWrapper<Item>().eq("itemid",itemid));
        if(itemService.remove(new QueryWrapper<Item>().eq("itemid",itemid))){

            if(item.getIavtar() != null){
                File file1 = new File(item.getIavtar());
                if (file1.exists()){//文件是否存在
                    if(file1.delete()){
                        System.out.println("delete avatar success");//删除文件
                    }
                }
            }

            if(item.getSubavatar() != null){
                File file2 = new File(item.getSubavatar());
                if (file2.exists()){//文件是否存在
                    if(file2.delete()){
                        System.out.println("delete subAvatar success");//删除文件
                    }
                }
            }
            return Result.succ("remove success");
        }else {
            return Result.fail("item not found");
        }
    }

    @ApiOperation(value = "关键词搜索商品")
    @PostMapping("/itemsearch")
    @ResponseBody
    public Result SearchItem(String itemname){
        List<Item> itemList = itemService.list();
        String keywords[];
        if(StringUtils.indexOf(itemname," ")>0){
            keywords = itemname.split(" ");
        }else {
            keywords = new String[]{itemname};
        }
        List<Item> itemReturnList = new ArrayList<>();
        for(Item itemt:itemList){
            boolean isExist = true;
            for(String words:keywords){
                if (StringUtils.isBlank(words)){
                    continue;
                }
                if(StringUtils.indexOf(itemt.getItemname(),words) == -1){
                    isExist = false;
                }
                System.out.println(itemt.getItemname()+"  "+words+"  "+isExist);
            }
            if (isExist){
                itemReturnList.add(itemt);
            }
        }

        if(itemReturnList.size()>0){
            return Result.succ("this is itemlist",itemReturnList);
        }else {
            return Result.fail("no item exist");
        }
    }

}
